﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Nodes;
using LHS.Core.TableInfo.SetUpCaseRecordForm;
using MS.Core.Base.Transaction;
using LHS.Core.CaseRecord.Controller;
using MS.Core.Base.Data;
using MS.Core.Constants;
using LHS.Core.CaseRecord.Classes;
using DevExpress.XtraTreeList;
using LHS.Core.TableInfo;

namespace LHS.Win.SetUpCaseRecordForm.Forms
{
    public partial class frmXayDungBieuMau : Form
    {
        #region constructor
        public frmXayDungBieuMau()
        {
            InitializeComponent();
            this._intCRC_ID = -1;
        }

        public frmXayDungBieuMau(int intCategoryID)
        {
            InitializeComponent();
            this._intCRC_ID = intCategoryID;
        }
        #endregion

        #region variables
        private int _intCRC_ID = -1;

        private frmMain _frmParent;
        private DataSet _dsCaseRecord;
        private DataTable _dtCaseRecord_Category;
        private DataTable _dtCaseRecord_Tree;
        private DataRow _drCaseRecord_Tree;
        private DataTable _dtCaseRecord_Leaf;
        private DataRow _drCaseRecord_Leaf;
        private DataTable _dtCaseRecord_Tree4Delete;
        private DataTable _dtCaseRecord_Leaf4Delete;
        private String _strPreValueTenHangMuc = "";
        private Boolean _blnSaveWhenChangeCategory = false;
        #endregion

        #region properties
        private CaseRecordController caseRecordController;
        private CaseRecordController _caseRecordController
        {
            get
            {
                if (caseRecordController == null)
                    caseRecordController = new CaseRecordController();
                return caseRecordController;
            }
        }
        #endregion

        #region controls events
        #region case record tree
        private void cboLoaiBieuMau_QueryPopUp(object sender, CancelEventArgs e)
        {
            cboLoaiBieuMau.Properties.PopupWidth = cboLoaiBieuMau.Width;
        }

        private void cboLoaiBieuMau_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if ((this._dtCaseRecord_Category != null && this._dtCaseRecord_Category.Select(this._caseRecordController.GetUpdatedRowSQL).Length > 0) ||
                    (this._dtCaseRecord_Tree != null && this._dtCaseRecord_Tree.Select(this._caseRecordController.GetUpdatedRowSQL).Length > 0) ||
                    (this._dtCaseRecord_Leaf != null && this._dtCaseRecord_Leaf.Select(this._caseRecordController.GetUpdatedRowSQL).Length > 0))
                    if (MessageBox.Show(clsConstant.MSG_Confirm_Before_Change_Surgical_Category, clsConstant.Title_CaseRecord_Builder, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        this._blnSaveWhenChangeCategory = true;
                        btnSave_Click(null, null);
                    }
                this._getData();
            }
            catch (Exception ex) { Class.ShowException(ex); }
        }

        private void treeCaseRecord_Tree_ShownEditor(object sender, EventArgs e)
        {
            try
            {
                if (this._drCaseRecord_Tree != null)
                {
                    this._strPreValueTenHangMuc = this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_NAME].ToString().Trim();
                }
                else
                {
                    this._strPreValueTenHangMuc = "";
                }
            }
            catch (Exception ex)
            {
                Class.ShowException(ex);
            }
        }

        private void treeCaseRecord_Tree_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            try
            {
                if (treeCaseRecord_Tree.FocusedNode != null)
                {
                    this.Text = _caseRecordTree_GetDisplayName(treeCaseRecord_Tree.FocusedNode);
                    this._drCaseRecord_Tree = ((DataRowView)(treeCaseRecord_Tree.GetDataRecordByNode(treeCaseRecord_Tree.FocusedNode))).Row;
                    this._drCaseRecord_Leaf = null;
                    Boolean blnIsUsed = false;
                    if (this._drCaseRecord_Tree != null)
                    {
                        this.chkIsLeaf.EditValue = Convert.ToInt32(this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_ISLEAF]).ToString();
                        if (chkIsLeaf.Checked)
                        {
                            if (this._dtCaseRecord_Leaf != null)
                            {
                                DataRow[] drFound = this._dtCaseRecord_Leaf.Select(CaseRecord_LeafInfo.CRT_ID + " = '" + this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_ID].ToString() + "'");
                                if (drFound.Length > 0)
                                    this._drCaseRecord_Leaf = drFound[0];
                                else
                                    _caseRecordLeaf_AddRow();
                            }
                            else
                            {
                                _caseRecordLeaf_AddRow();
                            }
                        }
                        if (Convert.ToInt32(this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_LEVEL]) == -1)
                        {
                            this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_LEVEL] = this.treeCaseRecord_Tree.FocusedNode.Level;
                            if (Convert.ToInt32(this._drCaseRecord_Tree[DBBaseInfo.Hidden_Status]) != Convert.ToInt32(GlobalEnumType.Status.REGISTER))
                                this._drCaseRecord_Tree[DBBaseInfo.Hidden_Status] = Convert.ToInt32(GlobalEnumType.Status.UPDATE);
                        }
                        blnIsUsed = (Convert.ToInt32(this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_ISUSED]) == Convert.ToInt32(clsEnumType.BooleanValue.TRUE));
                        //this.cboControlType_EditValueChanged(null, null);
                    }
                    this._caseRecordLeaf_SetData();

                    if (blnIsUsed)
                    {
                        this._lockControl4IsUsed();
                    }
                    else
                    {
                        this.chkIsLeaf.Enabled = !treeCaseRecord_Tree.FocusedNode.HasChildren;
                        this.btnTreeAddChild.Enabled = !this.chkIsLeaf.Checked;
                        this.btnTreeRemove.Enabled = true;
                        _setEnableControls(this.chkIsLeaf.Checked);
                    }
                }
            }
            catch (Exception ex)
            {
                Class.ShowException(ex);
            }
        }

        private void treeCaseRecord_Tree_CellValueChanged(object sender, DevExpress.XtraTreeList.CellValueChangedEventArgs e)
        {
            try
            {
                if (treeCaseRecord_Tree.FocusedColumn == tgcCRT_Name)
                {
                    if (this._drCaseRecord_Tree != null)
                    {
                        if (this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_NAME].ToString().Trim().Equals(""))
                        {
                            this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_NAME] = this._strPreValueTenHangMuc;
                        }
                        else
                        {
                            this._strPreValueTenHangMuc = this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_NAME].ToString().Trim();
                        }
                        if (Convert.ToInt32(this._drCaseRecord_Tree[DBBaseInfo.Hidden_Status]) != Convert.ToInt32(GlobalEnumType.Status.REGISTER))
                            this._drCaseRecord_Tree[DBBaseInfo.Hidden_Status] = Convert.ToInt32(GlobalEnumType.Status.UPDATE);
                    }
                }
            }
            catch (Exception ex)
            {
                Class.ShowException(ex);
            }
        }

        private void chkIsLeaf_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                this._setEnableControls(chkIsLeaf.Checked);
                if (this._drCaseRecord_Tree != null)
                {
                    this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_ISLEAF] = chkIsLeaf.EditValue;
                    if (chkIsLeaf.Checked)
                    {
                        _caseRecordLeaf_AddRow();
                        this.btnTreeAddChild.Enabled = false;
                    }
                    else
                    {
                        _caseRecordLeaf_DeleteRow();
                        this.btnTreeAddChild.Enabled = true;
                    }
                    this._caseRecordLeaf_SetData();
                    if (Convert.ToInt32(this._drCaseRecord_Tree[DBBaseInfo.Hidden_Status]) != Convert.ToInt32(GlobalEnumType.Status.REGISTER))
                        this._drCaseRecord_Tree[DBBaseInfo.Hidden_Status] = Convert.ToInt32(GlobalEnumType.Status.UPDATE);
                }
                this.btnPreviewAll.Enabled = this._dtCaseRecord_Leaf.Rows.Count > 0;
            }
            catch (Exception ex)
            {
                Class.ShowException(ex);
            }
        }

        private void btnTreeAdd_Click(object sender, EventArgs e)
        {
            try
            {
                _caseRecordTree_AddNode(false);
            }
            catch (Exception ex)
            {
                Class.ShowException(ex);
            }
        }

        private void btnTreeAddChild_Click(object sender, EventArgs e)
        {
            try
            {
                _caseRecordTree_AddNode(true);
            }
            catch (Exception ex)
            {
                Class.ShowException(ex);
            }
        }

        private void btnTreeRemove_Click(object sender, EventArgs e)
        {
            try
            {
                if (this._drCaseRecord_Tree != null)
                {
                    if (MessageBox.Show(clsConstant.MSG_ConfirmRemoveNode, this.cboLoaiBieuMau.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        _caseRecordTree_RemoveRow(this._drCaseRecord_Tree);
                        this._dtCaseRecord_Tree.AcceptChanges();
                        if (this._dtCaseRecord_Leaf != null)
                            this._dtCaseRecord_Leaf.AcceptChanges();
                        if (this._dtCaseRecord_Tree4Delete.Rows.Count > 0 || this._dtCaseRecord_Leaf4Delete.Rows.Count > 0)
                        {
                            TransactionManager.ThreadTransactionMgr().BeginTransaction();
                            this._caseRecordController.deleteCaseRecord_Tree(this._intCRC_ID.ToString(), this._dtCaseRecord_Tree4Delete, this._dtCaseRecord_Leaf4Delete);
                            TransactionManager.ThreadTransactionMgr().CommitTransaction();
                            this._dtCaseRecord_Tree4Delete.Clear();
                            this._dtCaseRecord_Leaf4Delete.Clear();
                        }

                        if (treeCaseRecord_Tree.FocusedNode != null)
                        {
                            this._drCaseRecord_Tree = ((DataRowView)(treeCaseRecord_Tree.GetDataRecordByNode(treeCaseRecord_Tree.FocusedNode))).Row;
                        }
                        else
                        {
                            this._drCaseRecord_Tree = null;
                        }
                        //MessageBox.Show(clsConstant.MSG_DeleteSuccessful);

                        if (this._dtCaseRecord_Tree.Rows.Count <= 0)
                        {
                            _getData();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                TransactionManager.ThreadTransactionMgr().RollbackTransaction();
                Class.ShowException(ex);
                _getData();
            }
        }

        private void btnPreviewAll_Click(object sender, EventArgs e)
        {
            try
            {
                frmBieuMau_1 myBieuMau = new frmBieuMau_1(this, this._intCRC_ID.ToString(), "-1", "-1", "-1", "-1", clsEnumType.DisplayMode.PREVIEW);
                myBieuMau.ShowDialog();
            }
            catch (Exception ex) { Class.ShowException(ex); }
        }
        #endregion

        #region case record leaf
        private void cboDataType_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (this._drCaseRecord_Leaf != null)
                {
                    this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRL_DATATYPE] = this.cboDataType.EditValue;

                    DataTable dtControlType = clsMasterData.dtControlType;
                    DataView dvControlType = new DataView(dtControlType);
                    dvControlType.RowFilter = clsMasterData.DATA_TYPE + " = '" + this.cboDataType.EditValue + "'";
                    this.cboControlType.Properties.DataSource = dvControlType;

                    if (dvControlType.Count > 0)
                    {
                        this.cboControlType.EditValue = this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRL_CONTROLTYPE].ToString();
                        if (this.cboControlType.ItemIndex < 0)
                            this.cboControlType.ItemIndex = 0;
                    }

                    if (this.cboDataType.EditValue.ToString().Equals(Convert.ToInt32(clsEnumType.DataType.TEXT).ToString()))
                        this.spnMaxLength.Enabled = true;
                    else
                    {
                        this.spnMaxLength.Enabled = false;
                        this.spnMaxLength.EditValue = 0;
                    }

                    if (Convert.ToInt32(this._drCaseRecord_Leaf[DBBaseInfo.Hidden_Status]) != Convert.ToInt32(GlobalEnumType.Status.REGISTER))
                        this._drCaseRecord_Leaf[DBBaseInfo.Hidden_Status] = Convert.ToInt32(GlobalEnumType.Status.UPDATE);
                }
            }
            catch (Exception ex)
            {
                Class.ShowException(ex);
            }
        }

        private void cboControlType_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.cboControlType.EditValue != null && !this.cboControlType.EditValue.Equals(""))
                {
                    if (this._drCaseRecord_Leaf != null)
                    {
                        this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRL_CONTROLTYPE] = this.cboControlType.EditValue;
                        _setVisibleControlsPreview(this.cboControlType.EditValue.ToString());
                        if (Convert.ToInt32(this._drCaseRecord_Leaf[DBBaseInfo.Hidden_Status]) != Convert.ToInt32(GlobalEnumType.Status.REGISTER))
                            this._drCaseRecord_Leaf[DBBaseInfo.Hidden_Status] = Convert.ToInt32(GlobalEnumType.Status.UPDATE);
                    }
                }
            }
            catch (Exception ex)
            {
                Class.ShowException(ex);
            }
        }

        private void spnMaxLength_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (this._drCaseRecord_Leaf != null)
                {
                    this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRL_MAXLENGTH] = this.spnMaxLength.EditValue;
                    if (Convert.ToInt32(this._drCaseRecord_Leaf[DBBaseInfo.Hidden_Status]) != Convert.ToInt32(GlobalEnumType.Status.REGISTER))
                        this._drCaseRecord_Leaf[DBBaseInfo.Hidden_Status] = Convert.ToInt32(GlobalEnumType.Status.UPDATE);
                }
            }
            catch (Exception ex)
            {
                Class.ShowException(ex);
            }
        }

        private void txtPreviewTextArea_QueryPopUp(object sender, CancelEventArgs e)
        {
            ((DevExpress.XtraEditors.MemoExEdit)(sender)).Properties.PopupStartSize = new Size(((DevExpress.XtraEditors.MemoExEdit)(sender)).Width, 100);
        }

        private void txtRadioGroupDisplayText_EditValueChanged(object sender, EventArgs e)
        {
            this.layPreviewRadioGroup.MinSize = this.layPreviewRadioGroup.Size = new Size(this.layPreviewRadioGroup.MinSize.Width, 25);
            this.myRadioGroupPreview.DisplayText = this.txtRadioGroupDisplayText.Text;
            int intRowCount = this.myRadioGroupPreview.Count / clsConstant.Radio_Group_ColumnNo;
            if (intRowCount * clsConstant.Radio_Group_ColumnNo < this.myRadioGroupPreview.Count)
                intRowCount++;
            if (intRowCount > 0)
                this.layPreviewRadioGroup.MinSize = this.layPreviewRadioGroup.Size = new Size(this.layPreviewRadioGroup.MinSize.Width, intRowCount * (intRowCount <= 2 ? 35 : (intRowCount <= 5 ? 30 : 25)));

            if (this._drCaseRecord_Leaf != null)
            {
                this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRL_TITLEFORRADIOGROUP] = this.txtRadioGroupDisplayText.Text;
                if (Convert.ToInt32(this._drCaseRecord_Leaf[DBBaseInfo.Hidden_Status]) != Convert.ToInt32(GlobalEnumType.Status.REGISTER))
                    this._drCaseRecord_Leaf[DBBaseInfo.Hidden_Status] = Convert.ToInt32(GlobalEnumType.Status.UPDATE);
            }
        }

        private void txtRadioGroupDisplayText_QueryPopUp(object sender, CancelEventArgs e)
        {
            ((DevExpress.XtraEditors.MemoExEdit)(sender)).Properties.PopupStartSize = new Size(((DevExpress.XtraEditors.MemoExEdit)(sender)).Width, 100);
        }

        private void myRadioGroupPreview_Load(object sender, EventArgs e)
        {
            this.myRadioGroupPreview.Columns = clsConstant.Radio_Group_ColumnNo;
        }
        #endregion

        #region others
        private void frmXayDungBieuMau_Load(object sender, EventArgs e)
        {
            try
            {
                this._frmParent = (frmMain)this.ParentForm;
                this.Width = this._frmParent.Width * 80 / 100;
                this.Height = this._frmParent.Height * 80 / 100;
                this.Left = (this._frmParent.Width - this.Width) / 2;
                this.Top = (this._frmParent.Height - this.Height) / 4;

                this.lblRadioGroupHelp1.Text = clsConstant.Radio_Group_TitleHelp1;
                this.lblRadioGroupHelp2.Text = clsConstant.Radio_Group_TitleHelp2;

                _setEnableControls(false);
                this.chkIsLeaf.Enabled = false;
                _bindData();
            }
            catch (Exception ex)
            {
                Class.ShowException(ex);
            }
        }

        private void frmXayDungBieuMau_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                if (this._frmParent != null && this._frmParent is frmMain)
                    this._frmParent.mnuXayDungBieuMau.Enabled = true;
            }
            catch (Exception ex) { Class.ShowException(ex); }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (this._dtCaseRecord_Category.Rows.Count == 0)
                {
                    DataRow drCaseRecord_Category = this._dtCaseRecord_Category.NewRow();
                    drCaseRecord_Category[CaseRecord_CategoryInfo.CRC_ID] = this._intCRC_ID;
                    drCaseRecord_Category[CaseRecord_CategoryInfo.CRC_TABLENAME] = clsUtils.getNameFromString(this.cboLoaiBieuMau.Text, true) + "_" + clsUtils.getRandomString();
                    drCaseRecord_Category[DBBaseInfo.Hidden_Status] = Convert.ToInt32(GlobalEnumType.Status.REGISTER);
                    this._dtCaseRecord_Category.Rows.Add(drCaseRecord_Category);
                }
                _caseRecordTree_BuildOrder();
                TransactionManager.ThreadTransactionMgr().BeginTransaction();
                _caseRecordController.insertUpdateCaseRecord_Tree(this._dsCaseRecord);
                TransactionManager.ThreadTransactionMgr().CommitTransaction();
                if (!this._blnSaveWhenChangeCategory)
                    MessageBox.Show(clsConstant.MSG_SaveSuccessful);
                this._blnSaveWhenChangeCategory = false;
            }
            catch (Exception ex)
            {
                TransactionManager.ThreadTransactionMgr().RollbackTransaction();
                Class.ShowException(ex);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }
        #endregion
        #endregion

        #region private functions
        #region others
        private void _bindData()
        {
            cboDataType.Properties.ValueMember = clsMasterData.ID;
            cboDataType.Properties.DisplayMember = clsMasterData.VALUE;
            cboDataType.Properties.Columns[0].FieldName = clsMasterData.VALUE;
            cboDataType.Properties.DataSource = clsMasterData.dtDataType;
            cboDataType.EditValue = DBNull.Value;

            cboControlType.Properties.ValueMember = clsMasterData.ID;
            cboControlType.Properties.DisplayMember = clsMasterData.VALUE;
            cboControlType.Properties.Columns[0].FieldName = clsMasterData.VALUE;
            cboControlType.Properties.DataSource = clsMasterData.dtControlType;
            cboControlType.EditValue = DBNull.Value;

            tgcCRT_Name.FieldName = CaseRecord_TreeInfo.CRT_NAME;
            treeCaseRecord_Tree.KeyFieldName = CaseRecord_TreeInfo.CRT_ID;
            treeCaseRecord_Tree.ParentFieldName = CaseRecord_TreeInfo.CRT_PARENTID;

            cboLoaiBieuMau.Properties.ValueMember = InfoSurgical.SURISN;
            cboLoaiBieuMau.Properties.DisplayMember = InfoSurgical.SURNAME;
            cboLoaiBieuMau.Properties.Columns[0].FieldName = InfoSurgical.SURNAME;
            DataTable dtSurgical = _caseRecordController.getSurgicalDT();
            cboLoaiBieuMau.Properties.DataSource = dtSurgical;
            if (dtSurgical.Rows.Count > 0)
            {
                if (this._intCRC_ID == -1)
                    this._intCRC_ID = Convert.ToInt32(dtSurgical.Rows[0][InfoSurgical.SURISN]);
                cboLoaiBieuMau.EditValue = this._intCRC_ID;
            }
        }

        private void _getData()
        {
            this._intCRC_ID = Convert.ToInt32(cboLoaiBieuMau.EditValue);
            this._dsCaseRecord = _caseRecordController.getCaseRecord_TreeDS(this._intCRC_ID.ToString());
            this._dtCaseRecord_Category = this._dsCaseRecord.Tables[CaseRecord_CategoryInfo.TABLENAME];
            this._dtCaseRecord_Tree = this._dsCaseRecord.Tables[CaseRecord_TreeInfo.TABLENAME];
            this._dtCaseRecord_Leaf = this._dsCaseRecord.Tables[CaseRecord_LeafInfo.TABLENAME];

            this.treeCaseRecord_Tree.DataSource = this._dtCaseRecord_Tree;
            this._dtCaseRecord_Leaf4Delete = this._dtCaseRecord_Leaf.Clone();
            this._dtCaseRecord_Tree4Delete = this._dtCaseRecord_Tree.Clone();

            if (this._dtCaseRecord_Tree.Rows.Count == 0)
            {
                this._drCaseRecord_Tree = null;
                this._drCaseRecord_Leaf = null;
                this.chkIsLeaf.Checked = false;
                this._lockControl4IsUsed();
                this._caseRecordLeaf_SetData();
                this.btnPreviewAll.Enabled = this.btnSave.Enabled = false;
            }
            else
                this.btnPreviewAll.Enabled = this.btnSave.Enabled = true;

            if (this._dtCaseRecord_Tree.Rows.Count <= 0)
                this.Text = clsConstant.Title_CaseRecord_Builder;
        }

        private void _setEnableControls(Boolean blnState)
        {
            this.cboDataType.Enabled = blnState;
            this.spnMaxLength.Enabled = blnState;
            this.cboControlType.Enabled = blnState;
            this.txtRadioGroupDisplayText.Properties.ReadOnly = !blnState;
        }

        private void _setVisibleControlsPreview(String strControlType)
        {
            if (strControlType.Equals(Convert.ToInt32(clsEnumType.ControlType.CHECKBOX).ToString()))
            {
                layPreviewNumeric.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layPreviewTextArea.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layPreviewTextBox.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layPreviewRadioGroup.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layRadioGroup_Help1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layRadioGroup_Help2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layRadioGroup_Title.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;

                chkPreviewCheckBox.Checked = true;
                layPreviewCheckBox.Text = this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_NAME].ToString();
                layPreviewCheckBox.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            }

            else if (strControlType.Equals(Convert.ToInt32(clsEnumType.ControlType.NUMERIC).ToString()))
            {
                layPreviewCheckBox.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layPreviewTextArea.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layPreviewTextBox.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layPreviewRadioGroup.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layRadioGroup_Help1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layRadioGroup_Help2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layRadioGroup_Title.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;

                layPreviewNumeric.Text = this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_NAME].ToString();
                layPreviewNumeric.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            }

            else if (strControlType.Equals(Convert.ToInt32(clsEnumType.ControlType.TEXTAREA).ToString()))
            {
                layPreviewCheckBox.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layPreviewNumeric.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layPreviewTextBox.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layPreviewRadioGroup.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layRadioGroup_Help1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layRadioGroup_Help2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layRadioGroup_Title.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;

                layPreviewTextArea.Text = this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_NAME].ToString();
                layPreviewTextArea.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            }

            else if (strControlType.Equals(Convert.ToInt32(clsEnumType.ControlType.TEXTBOX).ToString()))
            {
                layPreviewCheckBox.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layPreviewNumeric.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layPreviewTextArea.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layPreviewRadioGroup.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layRadioGroup_Help1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layRadioGroup_Help2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layRadioGroup_Title.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;

                layPreviewTextBox.Text = this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_NAME].ToString();
                layPreviewTextBox.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            }

            else if (strControlType.Equals(Convert.ToInt32(clsEnumType.ControlType.RADIOGROUP).ToString()))
            {
                layPreviewCheckBox.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layPreviewNumeric.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layPreviewTextArea.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layPreviewTextBox.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;

                layPreviewRadioGroup.Text = this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_NAME].ToString();
                myRadioGroupPreview.DisplayText = this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRL_TITLEFORRADIOGROUP].ToString();
                layPreviewRadioGroup.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                layRadioGroup_Help1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                layRadioGroup_Help2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                layRadioGroup_Title.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            }

            else
            {
                layPreviewCheckBox.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layPreviewNumeric.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layPreviewTextArea.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layPreviewTextBox.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layPreviewRadioGroup.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layRadioGroup_Help1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layRadioGroup_Help2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layRadioGroup_Title.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            }
        }

        private void _lockControl4IsUsed()
        {
            this.btnTreeAddChild.Enabled = false;
            this.btnTreeRemove.Enabled = false;
            this.chkIsLeaf.Enabled = false;
            this.cboControlType.Enabled = false;
            this.cboDataType.Enabled = false;
            this.spnMaxLength.Enabled = false;
            this.txtRadioGroupDisplayText.Properties.ReadOnly = true;
        }
        #endregion

        #region case record tree
        private void _caseRecordTree_AddNode(Boolean blnIsAddChild)
        {
            if (treeCaseRecord_Tree.AllNodesCount > 0)
            {
                if (this._drCaseRecord_Tree != null)
                {
                    _caseRecordTree_AddRow(this._intCRC_ID, "", clsUtils.getRandomString(), Convert.ToInt32(clsEnumType.BooleanValue.FALES).ToString(), Convert.ToInt32(clsEnumType.BooleanValue.FALES).ToString(), clsConstant.NewNodeName, blnIsAddChild ? this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_ID].ToString() : this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_PARENTID].ToString());
                    if (blnIsAddChild)
                    {
                        treeCaseRecord_Tree.FocusedNode.ExpandAll();
                    }
                }
            }
            else
            {
                _caseRecordTree_AddRow(this._intCRC_ID, "", clsUtils.getRandomString(), Convert.ToInt32(clsEnumType.BooleanValue.FALES).ToString(), Convert.ToInt32(clsEnumType.BooleanValue.FALES).ToString(), clsConstant.NewNodeName, this._intCRC_ID.ToString());
            }
            this.btnSave.Enabled = this._dtCaseRecord_Tree.Rows.Count > 0;
        }

        private void _caseRecordTree_AddRow(int intCRC_ID, String strCRT_DESCRIPTION, String strCRT_ID, String strCRT_ISLEAF, String strCRT_ISUSED, String strCRT_NAME, String strCRT_PARENTID)
        {
            if (this._dtCaseRecord_Tree == null)
            {
                this._dtCaseRecord_Tree = CaseRecord_TreeInfo.GetStructure();
            }
            if (treeCaseRecord_Tree.DataSource == null)
            {
                treeCaseRecord_Tree.DataSource = this._dtCaseRecord_Tree;
            }

            DataRow drNew = this._dtCaseRecord_Tree.NewRow();
            drNew[CaseRecord_TreeInfo.CRC_ID] = intCRC_ID;
            drNew[CaseRecord_TreeInfo.CRT_DESCRIPTION] = strCRT_DESCRIPTION;
            drNew[CaseRecord_TreeInfo.CRT_ID] = strCRT_ID;
            drNew[CaseRecord_TreeInfo.CRT_ISLEAF] = strCRT_ISLEAF;
            drNew[CaseRecord_TreeInfo.CRT_ISUSED] = strCRT_ISUSED;
            drNew[CaseRecord_TreeInfo.CRT_LEVEL] = -1;
            drNew[CaseRecord_TreeInfo.CRT_NAME] = strCRT_NAME;
            drNew[CaseRecord_TreeInfo.CRT_PARENTID] = strCRT_PARENTID;
            drNew[DBBaseInfo.Hidden_Status] = Convert.ToInt32(GlobalEnumType.Status.REGISTER);
            this._dtCaseRecord_Tree.Rows.Add(drNew);
        }

        private void _caseRecordTree_RemoveRow(DataRow drCaseRecord_Tree)
        {
            if (drCaseRecord_Tree != null)
            {
                String strIsLeaf = drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_ISLEAF].ToString();
                String strCRT_ID = drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_ID].ToString();
                DataRow[] drFoundArr_CaseRecord_Tree = _caseRecordTree_GetChildArrDR(drCaseRecord_Tree);
                this._dtCaseRecord_Tree4Delete.ImportRow(drCaseRecord_Tree);
                drCaseRecord_Tree.Delete();

                if (strIsLeaf.Equals(Convert.ToInt32(clsEnumType.BooleanValue.TRUE).ToString()))
                {
                    if (this._dtCaseRecord_Leaf != null && this._dtCaseRecord_Leaf.Rows.Count > 0)
                    {
                        DataRow[] drFoundArr_CaseRecord_Leaf = this._dtCaseRecord_Leaf.Select(CaseRecord_LeafInfo.CRT_ID + " = '" + strCRT_ID + "'");
                        if (drFoundArr_CaseRecord_Leaf.Length > 0)
                        {
                            foreach (DataRow drFound_CaseRecord_Leaf in drFoundArr_CaseRecord_Leaf)
                            {
                                this._dtCaseRecord_Leaf4Delete.ImportRow(drFound_CaseRecord_Leaf);
                                drFound_CaseRecord_Leaf.Delete();
                            }
                        }
                    }
                }

                if (drFoundArr_CaseRecord_Tree == null || drFoundArr_CaseRecord_Tree.Length == 0)
                {
                    return;
                }
                foreach (DataRow drFound_CaseRecord_Tree in drFoundArr_CaseRecord_Tree)
                    _caseRecordTree_RemoveRow(drFound_CaseRecord_Tree);
            }
        }

        private String _caseRecordTree_GetDisplayName(TreeListNode node)
        {
            if (node == null)
                return "";
            if (node.ParentNode == null)
                return "<" + node.GetDisplayText(CaseRecord_TreeInfo.CRT_NAME) + ">";
            return _caseRecordTree_GetDisplayName(node.ParentNode) + "|<" + node.GetDisplayText(CaseRecord_TreeInfo.CRT_NAME) + ">";
        }

        private DataRow[] _caseRecordTree_GetChildArrDR(DataRow drCaseRecordTree)
        {
            if (this._dtCaseRecord_Tree != null && drCaseRecordTree != null)
            {
                return this._dtCaseRecord_Tree.Select(CaseRecord_TreeInfo.CRT_PARENTID + " = '" + drCaseRecordTree[CaseRecord_TreeInfo.CRT_ID].ToString() + "' AND " + CaseRecord_TreeInfo.CRT_ID + " <> '" + drCaseRecordTree[CaseRecord_TreeInfo.CRT_ID].ToString() + "'");
            }
            return null;
        }

        private void _caseRecordTree_BuildOrder()
        {
            if (this._dtCaseRecord_Tree.Select(DBBaseInfo.Hidden_Status + " = '" + Convert.ToInt32(GlobalEnumType.Status.REGISTER).ToString() + "'").Length > 0)
            {
                DataTable dtTMP = this._dtCaseRecord_Tree.Clone();
                _caseRecordTree_GetDataTableByTree(treeCaseRecord_Tree.Nodes, dtTMP);
                int intBuildOrder = 0;
                foreach (DataRow drTMP in dtTMP.Rows)
                {
                    drTMP[CaseRecord_TreeInfo.CRT_BUILDORDER] = intBuildOrder++;
                    if (Convert.ToInt32(drTMP[DBBaseInfo.Hidden_Status]) != Convert.ToInt32(GlobalEnumType.Status.REGISTER))
                        drTMP[DBBaseInfo.Hidden_Status] = Convert.ToInt32(GlobalEnumType.Status.UPDATE);
                }

                this._dsCaseRecord.Tables.Remove(this._dtCaseRecord_Tree);
                this._dtCaseRecord_Tree = dtTMP;
                this._dsCaseRecord.Tables.Add(this._dtCaseRecord_Tree);
                this.treeCaseRecord_Tree.DataSource = this._dtCaseRecord_Tree;
            }
        }

        private void _caseRecordTree_GetDataTableByTree(TreeListNodes treeListNodes, DataTable dtResult)
        {
            if (treeListNodes == null || treeListNodes.Count == 0)
                return;
            foreach (TreeListNode treeNode in treeListNodes)
            {
                dtResult.ImportRow(((DataRowView)(treeListNodes.TreeList.GetDataRecordByNode(treeNode))).Row);
                if (treeNode.HasChildren)
                    _caseRecordTree_GetDataTableByTree(treeNode.Nodes, dtResult);
            }
        }
        #endregion

        #region case record leaf
        private void _caseRecordLeaf_AddRow()
        {
            if (this._drCaseRecord_Tree != null)
            {
                if (this._dtCaseRecord_Leaf == null)
                    this._dtCaseRecord_Leaf = CaseRecord_LeafInfo.GetStructure();

                DataRow[] drFoundArr_CaseRecord_Leaf = this._dtCaseRecord_Leaf.Select(CaseRecord_LeafInfo.CRT_ID + " = '" + this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_ID].ToString() + "'");
                if (drFoundArr_CaseRecord_Leaf.Length > 0)
                {
                    this._drCaseRecord_Leaf = drFoundArr_CaseRecord_Leaf[0];
                }
                else
                {
                    String strColName = clsUtils.getNameFromString(this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_NAME].ToString(), false);
                    if (_dtCaseRecord_Leaf.Select(CaseRecord_LeafInfo.CRL_COLNAME + " = '" + strColName + "'").Length > 0)
                        strColName += "_" + this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_ID].ToString();

                    this._drCaseRecord_Leaf = this._dtCaseRecord_Leaf.NewRow();
                    this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRL_COLNAME] = strColName;
                    this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRL_CONTROLNUMBER] = 1;
                    this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRL_TITLEFORRADIOGROUP] = "";
                    this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRL_CONTROLTYPE] = Convert.ToInt32(clsEnumType.ControlType.TEXTBOX).ToString();
                    this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRL_DATATYPE] = Convert.ToInt32(clsEnumType.DataType.TEXT).ToString();
                    this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRL_MAXLENGTH] = 50;
                    this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRT_ID] = this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_ID];
                    this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRC_ID] = this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRC_ID];
                    this._drCaseRecord_Leaf[DBBaseInfo.Hidden_Status] = Convert.ToInt32(GlobalEnumType.Status.REGISTER);
                    this._dtCaseRecord_Leaf.Rows.Add(this._drCaseRecord_Leaf);
                }
            }
        }

        private void _caseRecordLeaf_DeleteRow()
        {
            if (this._drCaseRecord_Tree != null)
            {
                if (this._drCaseRecord_Leaf == null || !this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRT_ID].ToString().Equals(this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_ID].ToString()))
                {
                    if (this._dtCaseRecord_Leaf != null)
                    {
                        DataRow[] drFound = this._dtCaseRecord_Leaf.Select(CaseRecord_LeafInfo.CRT_ID + " = '" + this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_ID].ToString() + "'");
                        if (drFound.Length > 0)
                            this._drCaseRecord_Leaf = drFound[0];
                    }
                }
                if (this._drCaseRecord_Leaf != null)
                {
                    if (this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRT_ID].ToString().Equals(this._drCaseRecord_Tree[CaseRecord_TreeInfo.CRT_ID].ToString()))
                    {
                        this._dtCaseRecord_Leaf.Rows.Remove(this._drCaseRecord_Leaf);
                        this._drCaseRecord_Leaf = null;
                    }
                }
            }
        }

        private void _caseRecordLeaf_SetData()
        {
            if (this._drCaseRecord_Leaf == null)
            {
                this.cboDataType.EditValue = DBNull.Value;
                this.cboControlType.EditValue = DBNull.Value;
                this.txtColumnName.Text = "";
                this.spnMaxLength.EditValue = 0;
                this.txtRadioGroupDisplayText.Text = "";
                _setVisibleControlsPreview("");
            }
            else
            {
                this.cboDataType.EditValue = this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRL_DATATYPE].ToString();
                this.cboControlType.EditValue = this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRL_CONTROLTYPE].ToString();
                this.txtColumnName.Text = this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRL_COLNAME].ToString();
                this.spnMaxLength.EditValue = Convert.ToInt32(this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRL_MAXLENGTH]);
                this.txtRadioGroupDisplayText.Text = this._drCaseRecord_Leaf[CaseRecord_LeafInfo.CRL_TITLEFORRADIOGROUP].ToString();
            }
        }
        #endregion
        #endregion
    }
}